﻿//------------------------------------------------------------
// AureFramework
// Developed By Drunk Fish
// GitHub: https://github.com/YYYurz
// Gitee: https://gitee.com/yyyurz
//------------------------------------------------------------

using AureFramework.ObjectPool;
using AureFramework.Resource;
using UnityEngine;

namespace AureFramework.Entity
{
	public sealed partial class EntityModule : AureFrameworkModule, IEntityModule
	{
		/// <summary>
		/// 内部实体对象
		/// </summary>
		private sealed class EntityObject : ObjectBase
		{
			/// <summary>
			/// 获取实体编号
			/// </summary>
			public int Id
			{
				get;
				private set;
			}

			/// <summary>
			/// 获取实体所属的实体组
			/// </summary>
			public IEntityGroup Group
			{
				get;
				private set;
			}

			/// <summary>
			/// 获取实体游戏物体
			/// </summary>
			public GameObject GameObject
			{
				get;
				private set;
			}

			/// <summary>
			/// 获取实体逻辑类
			/// </summary>
			public EntityLogic EntityLogic
			{
				get;
				private set;
			}

			/// <summary>
			/// 实体初始化
			/// </summary>
			/// <param name="group"></param>
			/// <param name="gameObject"></param>
			/// <param name="entityLogic"></param>
			public void OnInit(IEntityGroup group, GameObject gameObject, EntityLogic entityLogic)
			{
				Group = group;
				GameObject = gameObject;
				EntityLogic = entityLogic;
				EntityLogic.Group = Group;
				EntityLogic.OnInit();
			}

			/// <summary>
			/// 实体显示
			/// </summary>
			/// <param name="entityId"></param>
			/// <param name="userData"></param>
			public void OnShow(int entityId, object userData)
			{
				Id = entityId;
				EntityLogic.Id = entityId;
				EntityLogic.OnShow(userData);
				GameObject.transform.SetAsFirstSibling();
				GameObject.SetActive(true);
			}

			/// <summary>
			/// 实体轮询
			/// </summary>
			/// <param name="elapseTime"> 距离上一帧的流逝时间，秒单位 </param>
			/// <param name="realElapseTime"> 距离上一帧的真实流逝时间，秒单位 </param>
			public void OnUpdate(float elapseTime, float realElapseTime)
			{
				EntityLogic.OnUpdate(elapseTime, realElapseTime);
			}

			/// <summary>
			/// 实体物理轮询
			/// </summary>
			/// <param name="fixedElapseTime"></param>
			/// <param name="realFixedElapseTime"></param>
			public void OnFixedUpdate(float fixedElapseTime, float realFixedElapseTime)
			{
				EntityLogic.OnFixedUpdate(fixedElapseTime, realFixedElapseTime);
			}

			/// <summary>
			/// 实体隐藏
			/// </summary>
			public void OnHide()
			{
				GameObject.SetActive(false);
				EntityLogic.OnHide();
			}

			public override void OnRelease()
			{
				base.OnRelease();
				
				Aure.GetModule<IResourceModule>().ReleaseInstance(GameObject);
				Id = default;
				Group = default;
				GameObject = default;
				EntityLogic = default;
			}
		}	
	}
}